package com.xbongbong.paas.help.menu;

import com.xbongbong.admin.pojo.dto.MirrorRollBackDTO;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.enums.MenuTypeEnum;
import com.xbongbong.paas.pojo.dto.FormEntityAddDTO;
import com.xbongbong.paas.pojo.dto.FormExplainAddDTO;
import com.xbongbong.paas.pojo.dto.MenuFormAddDTO;
import com.xbongbong.paas.service.PaasFormExplainService;
import com.xbongbong.paas.service.PaasFormService;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.service.init.AbstractInitMenuHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.AccessLinkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.ExpenseManagementMenuEnum;
import com.xbongbong.pro.enums.menu.enums.SystemAppMenuEnum;
import com.xbongbong.pro.form.pojo.dto.FormAddBatchDTO;
import com.xbongbong.pro.form.pojo.vo.FormAddBatchVO;
import com.xbongbong.pro.formexplain.pojo.dto.FormExplainAddBatchDTO;
import com.xbongbong.pro.formexplain.pojo.vo.FormExplainAddBatchVO;
import com.xbongbong.pro.fundset.pojo.FundSetTransitionPojo;
import com.xbongbong.pro.menu.pojo.dto.MenuFormAddBatchDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuFormAddBatchVO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 资金管理应用生成和菜单初始化
 * @author 吴峰
 * @version v1.0
 * @date 2019/1/17 19:46
 * @since v1.0
 */
@Component
public class InitExpenseManagementMenuHelp extends AbstractInitMenuHelp {

    private static final Logger LOG = LoggerFactory.getLogger(InitExpenseManagementMenuHelp.class);

    @Resource
    private PaasMenuService paasMenuService;
    @Resource
    private PaasFormService paasFormService;
    @Resource
    private PaasFormExplainService paasFormExplainService;
    @Resource
    private InitMenuHelp initMenuHelp;

    @Override
    public List<String> alias() {
        return Arrays.asList(SystemAppMenuEnum.EXPENSE_MANAGEMENT.getAlias());
    }
    @Override
    public MenuInitVO initApp(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        return initMenu(menuInitDTO, appMap);
    }

    /**
     * 资金管理菜单初始化
     * @author 吴峰
     * @date 2019/1/24 18:44
     * @param menuInitDTO
     * @return void
     * @throws XbbException
     */
    @Override
    public MenuInitVO initMenu(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        MenuInitVO menuInitVO = new MenuInitVO();
        try {
            // 获取下应用id
            menuInitDTO.setAppId(menuInitDTO.getAppMap().get(SystemAppMenuEnum.EXPENSE_MANAGEMENT.getAlias()).getId());

            // 插入除分组外对应的菜单
            MenuFormAddBatchVO menuFormAddBatchVO = formAddBatch(menuInitDTO);
            if (Objects.isNull(menuFormAddBatchVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            List<PaasMenuEntity> menuList = menuFormAddBatchVO.getMenuList();
            // 插入菜单对应的表单
            FormAddBatchDTO formAddBatchDTO = new FormAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, formAddBatchDTO);
            List<FormEntityAddDTO> formList = new ArrayList<>();
            // 通过分组菜单，得到需要插入的表单列表
            getFormList(formList, menuList);
            formAddBatchDTO.setFormList(formList);
            FormAddBatchVO formAddBatchVO = paasFormService.addBatch(formAddBatchDTO);
            if (Objects.isNull(formAddBatchVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            // 插入表单对应的字段解释
            formExplainAddBatch(menuInitDTO, formAddBatchVO);
            menuInitVO.setFormList(formAddBatchVO.getFormList());
        } catch (XbbException e){
            throw e;
        } catch (Exception e){
            LOG.error("InitCrmMenuHelp.menuInit初始化菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuInitVO;
    }

    @Override
    public void rollBackInit(MirrorRollBackDTO mirrorRollBackDTO, List<Long> installFormList, PaasFormEntity paasFormEntity) throws XbbException {
        return;
    }

    /**
     * 添加表单字段解释
     * @author 吴峰
     * @date 2019/1/24 18:44
     * @param menuInitDTO
     * @param formAddBatchVO 表单数据
     * @return 返回表单字段对象
     * @throws XbbException
     */
    public FormExplainAddBatchVO formExplainAddBatch(MenuInitDTO menuInitDTO, FormAddBatchVO formAddBatchVO) throws XbbException {
        FormExplainAddBatchVO formExplainAddBatchVO;
        try {
            List<PaasFormEntity> formResultList = formAddBatchVO.getFormList();
            FormExplainAddBatchDTO formExplainAddBatchDTO = new FormExplainAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, formExplainAddBatchDTO);
            List<FormExplainAddDTO> formExplainList = new ArrayList<>();
            FundSetTransitionPojo fundSetTransitionPojo = new FundSetTransitionPojo(menuInitDTO.getCorpid());
            for (PaasFormEntity item : formResultList) {
                FormExplainAddDTO formExplainAddDTO = new FormExplainAddDTO();
                BeanUtil.copyProperties(item, formExplainAddDTO);
                formExplainAddDTO.setFormId(item.getId());
                String alias = XbbRefTypeEnum.getByCode(item.getBusinessType()).getAlias();
                // 初始化方法获取字段列表
                formExplainAddDTO.setFormList(InitExplainsHelper.initFieldAttrEntity(alias, menuInitDTO.getProductForm(), fundSetTransitionPojo, 0));
                //默认启用，防止不启用的菜单覆盖
                formExplainAddDTO.setEnable(1);
                formExplainList.add(formExplainAddDTO);
            }
            formExplainAddBatchDTO.setFormExplainList(formExplainList);
            formExplainAddBatchVO = paasFormExplainService.addBatch(formExplainAddBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.formExplainAddBatch批量添加表单解释信息出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return formExplainAddBatchVO;
    }

    /**
     * 批量添加子级菜单
     * @author 吴峰
     * @date 2019/1/24 18:43
     * @param menuInitDTO
     * @return com.xbongbong.pro.menu.pojo.vo.MenuFormAddBatchVO
     * @throws XbbException
     */
    private MenuFormAddBatchVO formAddBatch(MenuInitDTO menuInitDTO) throws XbbException {
        MenuFormAddBatchVO menuFormAddBatchVO = new MenuFormAddBatchVO();
        try {
            Long appId = menuInitDTO.getAppId();

            MenuFormAddBatchDTO crmFormAddBatchDTO = new MenuFormAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, crmFormAddBatchDTO);
            crmFormAddBatchDTO.setAppId(appId);
            List<MenuFormAddDTO> crmFormMenuList = new ArrayList<>();
            for(ExpenseManagementMenuEnum menuEnum: ExpenseManagementMenuEnum.values()){
                if(menuEnum.getBelongAlias().equals(menuInitDTO.getAlias())
                        &&StringUtil.isNotEmpty(menuEnum.getParent())){
                    MenuFormAddDTO menuFormAddDTO = new MenuFormAddDTO();
                    BeanUtil.copyProperties(menuInitDTO, menuFormAddDTO);
                    menuFormAddDTO.setAppId(appId);
                    menuFormAddDTO.setParentId(0L);
                    menuFormAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                    menuFormAddDTO.setAlias(menuEnum.getAlias());
                    menuFormAddDTO.setName(menuEnum.getName());
                    AccessLinkEnum accessLinkEnum = menuEnum.getAccessLinkEnum();
                    menuFormAddDTO.setIcon(accessLinkEnum.getIcon());
                    menuFormAddDTO.setColor(accessLinkEnum.getColor());
                    menuFormAddDTO.setUrl(accessLinkEnum.getUrl());
                    menuFormAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                    menuFormAddDTO.setType(menuEnum.getType());
                    crmFormMenuList.add(menuFormAddDTO);
                }
            }
            if (CollectionsUtil.isNotEmpty(crmFormMenuList)) {
                crmFormAddBatchDTO.setMenuList(crmFormMenuList);
                menuFormAddBatchVO = paasMenuService.addBatchFormMenu(crmFormAddBatchDTO);
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitCrmMenuHelp.formAddBatch批量添加表单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuFormAddBatchVO;
    }

    /**
     * 根据菜单获取表单列表
     * @author 吴峰
     * @date 2019/1/24 18:43
     * @param formList 表单列表
     * @param menuList 菜单列表
     * @return 返回需要插入的表单列表
     * @throws XbbException 业务异常
     */
    public List<FormEntityAddDTO> getFormList(List<FormEntityAddDTO> formList, List<PaasMenuEntity> menuList) throws XbbException{
        try {
            for (PaasMenuEntity item : menuList) {
                FormEntityAddDTO formEntityAddDTO = new FormEntityAddDTO();
                BeanUtil.copyProperties(item, formEntityAddDTO);
                ExpenseManagementMenuEnum menuEnum = ExpenseManagementMenuEnum.getByAlias(item.getAlias());
                if (Objects.isNull(menuEnum)) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.BUSINESS_TYPE_NOT_EXIST);
                }
                if(MenuTypeEnum.GROUP.getType().equals(menuEnum.getType()) || MenuTypeEnum.MENU.getType().equals(menuEnum.getType())) {
                    continue;
                }
                formEntityAddDTO.setBusinessType(menuEnum.getCode());
                formEntityAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                formEntityAddDTO.setCustomTitle(getAppDefaultTitle(menuEnum.getCode()));
                formEntityAddDTO.setSummaryEntity(getAppDefaultSummary(menuEnum.getCode()));
                formEntityAddDTO.setLabels(getAppDefaultLabel(menuEnum.getCode()));
                formEntityAddDTO.setBusinessRules("0");
                formEntityAddDTO.setMenuId(item.getId());
                //默认启用，防止不启用的菜单覆盖
                formEntityAddDTO.setEnable(1);
                formList.add(formEntityAddDTO);
            }
        }catch (XbbException e) {
            throw e;
        }
        return formList;
    }
}
